package com.jymf.util;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 转json时候date类型的处理类
 */
public class DateJsonValueProcessor implements JsonValueProcessor {

	private String format = "yyyy-MM-dd HH:mm:ss";
	public DateJsonValueProcessor(){
		super();
	}

	public Object processArrayValue(Object value, JsonConfig config) {
		String[] obj = {};  
        if (value!=null&&(value instanceof Date[])) {  
            SimpleDateFormat sf = new SimpleDateFormat(format);  
            Date[] dates = (Date[]) value;  
            obj = new String[dates.length];  
            for (int i = 0; i < dates.length; i++) {  
                obj[i] = sf.format(dates[i]);  
            }  
        }  
        return obj; 
	}

	public Object processObjectValue(String key, Object value, JsonConfig config) {
		
		  if (value!=null&&(value instanceof Date)) {
	            String str = new SimpleDateFormat(format).format((Date) value);  
	            return str;  
	        }  
	        return String.valueOf(value);  
	}

	public String getFormat() {  
        return format;  
    }  
  
    public void setFormat(String format) {  
        this.format = format;  
    }  

}
